#ifndef EDITORSMANAGER_H
#define EDITORSMANAGER_H

#include <QWidget>
#include <QDebug>
#include <QFileSystemWatcher>
#include "Include/IEditorsManager.h"
#include "Include/Plugins.h"
#include "CurrentEditor.h"
//#include "Include/IEditor.h"

namespace Plugins
{
    class IEditor;
    class Project;
}

class QStackedWidget;
class HistoryNavigate;
class FindDialog;
class OpenedDocsWidget;
class QAbstractTableModel;

class EditorsManager : public QWidget, public Plugins::IEditorsManager
{
    Q_OBJECT
    Q_INTERFACES(Plugins::IEditorsManager)
public:
    explicit EditorsManager(QWidget *parent = 0);
    virtual ~EditorsManager();

public:
    virtual QWidget * widget(){ return this; }
    virtual QWidget * openedDocsWidget();
    virtual QStringList openedEditors();
    virtual bool isEditorOpened(const QString& file);

    virtual PluginWrapper<Plugins::IEditor> currentEditor();
    virtual void setProject(Plugins::Project *);
signals:
    void undoAvailable(bool);
    void redoAvailable(bool);
    void copyAvailable(bool);
    void pasteAvailable(bool);
    void activated(const QString & file);
    void errorListChanged(QAbstractTableModel*);
    void findAvailable(bool);
    void replaceAvailable(bool);
    void modifyChanged(const QString& file, bool m);
    void modifyChanged(bool m);
    void opened(const QString& file);
    void closed(const QString& file);
    void severityChanged(const QString&, int);
    void fileNavigate(const QString&, int);
public slots:
    virtual void openEditor(const QString& file, int offset = 0, bool offsetIsLine = false);
    virtual void closeEditor(const QString& file);
    virtual void closeCurrentEditor();
    virtual bool activateEditor(const QString& file);
    virtual void closeAllEditors();
    void jumpCurrentFileLine(int);
    void jumpToFileLine(const QString& ,int);
public slots:
    void undo();
    void redo();
    void copy();
    void cut();
    void paste();
    void del();
    void selectAll();
    void showFind();
    void resetEditor();
    void findNext();
    void findPrevious();
    void replace();
    void replaceFind();
    void replaceFindPrevious();
    void replaceAll();
    void uppercase();
    void lowercase();
    void capitalize();
    void save();
    void saveAs();
    void openFile();
private slots:
    void historyNavigate();
    void delNavigator(const QString&);
    void onModifyChanged(bool);
    void onSeverityChanged(int);
    void onFileChanged();//const QString & file
    void _onFileChanged(const QString & file);
    void onErrorListChanged(QAbstractTableModel*);
private:
    bool _closeEditor(const QString& file, bool activate = true);
    bool _openEditor(const QString& file);
private:
    QStackedWidget *_place;
    QMap< QString, PluginWrapper<Plugins::IEditor> > _editors;
    QString _currentFile;
    QStringList _stack;
    CurrentEditor _currentEditor;
    HistoryNavigate *_historyNavi;
    FindDialog *_find;
    Plugins::Project *_project;
    OpenedDocsWidget *_oDocs;
    QFileSystemWatcher *_watcher;
    QStringList _changedFiles;
};

#endif // EDITORSMANAGER_H
